<?php
/**
* @file $Id: AttendanceCodes.php 161 2006-09-07 06:21:17Z doritojones $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if(!isset($_REQUEST['table']))
	$_REQUEST['table'] = 0;

if($_REQUEST['values'] && $_POST['values'])
{
	foreach($_REQUEST['values'] as $id=>$columns)
	{
		if($columns['DEFAULT_CODE'] == 'Y')
			DBQuery("UPDATE ATTENDANCE_CODES SET DEFAULT_CODE=NULL WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND TABLE_NAME='".$_REQUEST['table']."'");

		if($id!='new')
		{
			$sql = "UPDATE ATTENDANCE_CODES SET ";
							
			foreach($columns as $column=>$value)
			{
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			}
			$sql = substr($sql,0,-1) . " WHERE ID='$id'";
			DBQuery($sql);
		}
		else
		{
			$sql = "INSERT INTO ATTENDANCE_CODES ";

			$fields = 'ID,SCHOOL_ID,SYEAR,TABLE_NAME,';
			$values = db_seq_nextval('ATTENDANCE_CODES_SEQ').",'".UserSchool()."','".UserSyear()."','".$_REQUEST['table']."',";

			$go = 0;
			foreach($columns as $column=>$value)
			{
				if(isset($value) && $value!='')
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
			
			if($go)
				DBQuery($sql);
		}
	}
}

DrawHeader(ProgramTitle());

if($_REQUEST['new_category_title'])
{
	$id = DBGet(DBQuery("SELECT ".db_seq_nextval('ATTENDANCE_CODE_CATEGORIES_SEQ').' AS ID'.FROM_DUAL));
	$id = $id[1]['ID'];

	DBQuery("INSERT INTO ATTENDANCE_CODE_CATEGORIES (SYEAR,SCHOOL_ID,ID,TITLE) values('".UserSyear()."','".UserSchool()."',".$id.",'".$_REQUEST['new_category_title']."')");
	$_REQUEST['table'] = $id;
}

if($_REQUEST['modfunc']=='remove')
{
	if($_REQUEST['id'])
	{
		if(DeletePrompt(_('attendance code')))
		{
			DBQuery("DELETE FROM ATTENDANCE_CODES WHERE ID='$_REQUEST[id]'");
			unset($_REQUEST['modfunc']);
		}
	}
	elseif($_REQUEST['table'])
	{
		if(DeletePrompt(_('category')))
		{
			DBQuery("DELETE FROM ATTENDANCE_CODE_CATEGORIES WHERE ID='$_REQUEST[table]'");
			unset($_REQUEST['modfunc']);
			$_REQUEST['table'] = '0';
		}
	}
}

if($_REQUEST['modfunc']!='remove')
{
	if($_REQUEST['table']!=='new')
	{
		$sql = "SELECT ID,TITLE,SHORT_NAME,TYPE,DEFAULT_CODE,STATE_CODE,SORT_ORDER FROM ATTENDANCE_CODES WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND TABLE_NAME='".$_REQUEST['table']."' ORDER BY SORT_ORDER,TITLE";
		$QI = DBQuery($sql);
		$attendance_codes_RET = DBGet($QI,array('TITLE'=>'_makeTextInput','SHORT_NAME'=>'_makeTextInput','SORT_ORDER'=>'_makeTextInput','TYPE'=>'_makeSelectInput','STATE_CODE'=>'_makeSelectInput','DEFAULT_CODE'=>'_makeCheckBoxInput'));
	}
	
	$columns = array('TITLE'=>_('Title'),'SHORT_NAME'=>_('Short Name'),'SORT_ORDER'=>_('Sort Order'),'TYPE'=>_('Type'),'DEFAULT_CODE'=>_('Default for Teacher'),'STATE_CODE'=>_('State Code'));
	if($_REQUEST['table']!='0')
		unset($columns['STATE_CODE']);
	$link['add']['html'] = array('TITLE'=>_makeTextInput('','TITLE'),'SHORT_NAME'=>_makeTextInput('','SHORT_NAME'),'SORT_ORDER'=>_makeTextInput('','SORT_ORDER'),'TYPE'=>_makeSelectInput('','TYPE'),'DEFAULT_CODE'=>_makeCheckBoxInput('','DEFAULT_CODE'),'STATE_CODE'=>_makeSelectInput('','STATE_CODE'));
	$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=remove";
	$link['remove']['variables'] = array('id'=>'ID');
	
	echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=update&table=$_REQUEST[table] method=POST>";
	DrawHeader('',SubmitButton(_('Save')));

	$tabs = array(array('title'=>_('Attendance'),'link'=>"Modules.php?modname=$_REQUEST[modname]&table=0"));
	$categories_RET = DBGet(DBQuery("SELECT ID,TITLE FROM ATTENDANCE_CODE_CATEGORIES WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
	foreach($categories_RET as $category)
		$tabs[] = array('title'=>$category['TITLE'],'link'=>"Modules.php?modname=$_REQUEST[modname]&table=".$category['ID']);

	if($_REQUEST['table']==='new')
		$tabs[] = array('title'=>button('white_add'),'link'=>"Modules.php?modname=$_REQUEST[modname]&table=new");
	else
		$tabs[] = array('title'=>button('add'),'link'=>"Modules.php?modname=$_REQUEST[modname]&table=new");

	echo '<BR>';

	if($_REQUEST['table']!=='new')
	{
		if(count($attendance_codes_RET)==0)
		{
			$_FOCUS['selected_tab'] = "Modules.php?modname=$_REQUEST[modname]&table=$_REQUEST[table]";
			echo PopTable('header',$tabs);
			ListOutput($attendance_codes_RET,$columns,'','',$link,array(),array('download'=>false,'search'=>false));
			if($_REQUEST['table']!=0)
				echo '<CENTER>'.button('remove',_('Delete this category'),"Modules.php?modname=$_REQUEST[modname]&modfunc=remove&table=$_REQUEST[table]");
			echo '<BR><CENTER>'.SubmitButton(_('Save')).'</CENTER>';
			echo PopTable('footer');
		}
		else
		{
			echo '<CENTER>'.WrapTabs($tabs,"Modules.php?modname=$_REQUEST[modname]&table=$_REQUEST[table]").'</CENTER>';
			ListOutput($attendance_codes_RET,$columns,'','',$link,array(),array('download'=>false,'search'=>false));
			echo '<BR><CENTER>'.SubmitButton(_('Save')).'</CENTER>';
		}
	}
	else
	{
		$_FOCUS['selected_tab'] = "Modules.php?modname=$_REQUEST[modname]&table=$_REQUEST[table]";
		echo PopTable('header',$tabs);
		echo '<CENTER>'._('New Category Title').' <INPUT type=text name=new_category_title></CENTER>';
		echo '<BR><CENTER>'.SubmitButton(_('Save')).'</CENTER>';
		echo PopTable('footer');
	}
	echo '</FORM>';
}

function _makeTextInput($value,$name)
{	global $THIS_RET;
	
	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
		$id = 'new';
	
	if($name=='SHORT_NAME' || $name=='SORT_ORDER')
		$extra = 'size=5 maxlength=10';
	
	return TextInput($value,'values['.$id.']['.$name.']','',$extra);
}

function _makeSelectInput($value,$name)
{	global $THIS_RET;
	
	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
		$id = 'new';
	
	if($name=='TYPE')
		$options = array('teacher'=>_('Teacher & Office'),'official'=>_('Office Only'));
	elseif($name='STATE_CODE')
		$options = array('P'=>_('Present'),'A'=>_('Absent'),'H'=>_('Half'));
	
	return SelectInput($value,'values['.$id.']['.$name.']','',$options);
}

function _makeCheckBoxInput($value,$name)
{	global $THIS_RET;

	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
	{
		$id = 'new';
		$new = true;
	}
	
	return CheckBoxInput($value,'values['.$id.']['.$name.']','','',$new);
}
?>